<?php

/**
 * UserIdentity.php
 *
 * This class represents a user identity and this is able to authenticate a user.
 *
 * @author: Hemendra <hemendra@t9l.com>
 * 
 */
class UserIdentity extends CUserIdentity {

    /**
     * @var integer id of logged user
     */
    private $_id;

    const ERROR_STATUS_NOTACTIVE = 4;

    /**
     * Authenticates username and password
     * @return boolean CUserIdentity::ERROR_NONE if successful authentication
     */
    public function authenticate() {
        $user = Users::model()->find(array('condition' => 'username=:loginname', 'params' => array(':loginname' => $this->username)));
        //echo  $user->id;die;
        if ($user === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else if ($user->status == 0) {
            $this->errorCode = self::ERROR_STATUS_INVALID;
        } else if ($user->password != md5($this->password)) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        } else {
            $this->_id = $user->id;
            $this->username = $user->username;
            $this->errorCode = self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

    public function trainerauthenticate() {
        $user = Users::model()->find(array('condition' => 'username=:loginname', 'params' => array(':loginname' => $this->username)));
        //echo  $user->id;die;
        if ($user === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else if ($user->password != md5($this->password)) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        } else if ($user->status != 0) {
            $this->errorCode = self::ERROR_STATUS_INVALID;
        } else {
            $this->_id = $user->id;
            $this->username = $user->username;
            $this->errorCode = self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

    public function authenticateshoppng() {
        $user = Users::model()->find(array('condition' => 'username=:loginname', 'params' => array(':loginname' => $this->username)));
        //echo  $user->id;die;
        if ($user === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else if ($user->password != $this->password) {
            echo $user->password . '<br>' . $this->password;
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        }
//		else if ($user->status == 0) {
//                   	$this->errorCode=self::ERROR_STATUS_INVALID;
//		} 
        else {
            $this->_id = $user->id;
            $this->username = $user->username;
            $this->errorCode = self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

    /**
     * Creates an authenticated user with no passwords for registration
     * process (checkout)
     * @param string $username
     * @return self
     */
    public static function createAuthenticatedIdentity($id, $username) {
        $identity = new self($username, '');
        $identity->_id = $id;
        $identity->errorCode = self::ERROR_NONE;
        return $identity;
    }

    /**
     *
     * @return integer id of the logged user, null if not set
     */
    public function getId() {
        return $this->_id;
    }

}